package com.marui.ch8a.marui.vo;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;

public class MJobInfo<R> {
    private final String jobName;
    private final int jobLength;
    private final MITaskProcesser<?,?> taskProcesser;
    private final AtomicInteger successCount;
    private final AtomicInteger taskProcesserCount;
    private final LinkedBlockingDeque<MTaskResult<R>> taskDetailQueue;
    private final long expireTime;

    public MJobInfo(String jobName, int jobLength, MITaskProcesser<?, ?> taskProcesser, long expireTime) {
        this.jobName = jobName;
        this.jobLength = jobLength;
        this.taskProcesser = taskProcesser;
        this.successCount = new AtomicInteger(0);
        this.taskProcesserCount = new AtomicInteger(0);
        this.taskDetailQueue = new LinkedBlockingDeque<MTaskResult<R>>(jobLength);;
        this.expireTime = expireTime;
    }

    public MITaskProcesser<?,?> getTaskProcesser(){
        return taskProcesser;
    }
    //提供工作中失败的次数，课堂上没有加，只是为了方便调用者使用
    public int getFailCount() {
        return taskProcesserCount.get() - successCount.get();
    }

    public int getSuccessCount() {
        return successCount.get();
    }

    public int getTaskProcesserCount() {
        return taskProcesserCount.get();
    }
    public String getTotalProcess() {
        return "Success["+successCount.get()+"]/Current["
                +taskProcesserCount.get()+"] Total["+jobLength+"]";
    }

    public List<MTaskResult<R>> getTaskDetail(){
        List<MTaskResult<R>> list = new LinkedList<>();
        MTaskResult<R> taskResult;
        while ((taskResult=taskDetailQueue.peekFirst())!=null){
            list.add(taskResult);
        }
        return list;
    }

    public void addTaskResult(MTaskResult<R> result){
        if(result.getmTaskResultType().equals(MTaskResultType.SUCCESS)){
            successCount.incrementAndGet();
        }
        taskDetailQueue.addLast(result);
        taskProcesserCount.incrementAndGet();
    }


}
